package net.lainiao.zhihu.reptitle;

import com.meterware.httpunit.HttpUnitOptions;
import com.meterware.httpunit.WebConversation;
import com.meterware.httpunit.WebResponse;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.xml.sax.SAXException;

import java.io.IOException;

/**
 * Html公共服务类
 */
public class HtmlCommon {
    public static WebConversation webConversation = new WebConversation();

    static {
        String IP = NetCommon.getLocalIP();
        HttpUnitOptions.setScriptingEnabled(false);
        if (IP.indexOf("10.71.197") > -1) {
            webConversation.set_readTimeout(10000);
            webConversation.setProxyServer("10.22.96.29", 8080);
        }
    }

    /**
     * 根据URL获取HTML
     *
     * @param url
     * @return
     */
    public  static Document getDocument(String url) throws IOException, SAXException, InterruptedException {
        WebResponse wr = webConversation.getResponse(url);
        String html = getHtml(url);
        Document document = Jsoup.parse(html);
        return document;
    }


    public synchronized static String getHtml(String url) throws IOException, SAXException, InterruptedException {
        WebResponse wr = webConversation.getResponse(url);
        String html = wr.getText();
        Thread.sleep(1000);
        return html;
    }
}
